package edu.dgut.bbssystem.tff;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 *  Set/HashSet集合的使用
 *  HashSet：查找效率高
 * @author Administrator
 *
 */
public class SetDemo {

    public static void main(String[] args) {
        Set<StudentDemo> stuSet = new HashSet<StudentDemo>();
        StudentDemo student1 = new StudentDemo("宋小宝","20140163",90);
        StudentDemo student2 = new StudentDemo("赵本山","20140235",85);
        StudentDemo student3 = new StudentDemo("黄晓明","20140196",80);
        StudentDemo student4 = new StudentDemo("张一山","20140345",86);
        StudentDemo student5 = new StudentDemo("黄   磊","20140145",96);
        StudentDemo student6 = new StudentDemo("沙   溢","20140526",70);

        stuSet.add(student1);
        stuSet.add(student2);
        stuSet.add(student3);
        stuSet.add(student4);
        stuSet.add(student5);
        stuSet.add(student6);

        System.out.println("***********查找***********");
        System.out.println(stuSet.contains(student6));

        System.out.println("***********遍历*************");
        System.out.println("******方法一：******");
        //使用增强for循环遍历集合,由于Set集合没有get()方法，所有无法使用普通for循环遍历
        for (Object obj : stuSet) {
            StudentDemo sDemo = (StudentDemo)obj;
            System.out.println("姓名："+sDemo.getStuName()+"-学号："+sDemo.getStuId()+"-成绩："+sDemo.getStuGrade());
        }

        System.out.println("******方法二：******");
        //使用Iterator迭代器遍历Set集合
        Iterator iterator = stuSet.iterator();
        while (iterator.hasNext()) {
            StudentDemo stuIt = (StudentDemo)iterator.next();
            System.out.println("姓名："+stuIt.getStuName()+"-学号："+stuIt.getStuId()+"-成绩："+stuIt.getStuGrade());
        }

    }

}
